package com.paomi.client.activity.vip;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.paomi.client.R;
import com.paomi.client.activity.vip.adapter.CouponListAdapter;
import com.paomi.client.activity.vip.adapter.NextCouponListAdapter;
import com.paomi.client.activity.vip.model.CouponBean;
import com.paomi.client.activity.vip.model.MyCounponBean;
import com.paomi.client.activity.vip.view.MyVipView;
import com.paomi.client.base.BaseMvpActivity;
import com.paomi.client.bean.BaseJSON;
import com.paomi.client.utils.ToastUtil;
import com.paomi.client.utils.UIUtils;
import com.paomi.client.view.LoadingDialog;
import com.paomi.client.view.ProgressBubbleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MyVipActivity extends BaseMvpActivity<MyVipView, MyVipPresenter> implements MyVipView {

    @BindView(R.id.seek_pro)
    ProgressBubbleView seekBar;
    @BindView(R.id.next_btn)
    TextView nextBtn;
    @BindView(R.id.my_vip_list)
    RecyclerView myVipList;
    @BindView(R.id.next_btn_level)
    TextView nextBtnLevel;
    @BindView(R.id.level_vip_list)
    RecyclerView levelVipList;
    @BindView(R.id.vip_name)
    TextView vipName;//会员名称
    @BindView(R.id.update_txt)
    TextView updateTxt;//升级提示
//    @BindView(R.id.scale)
//    IndicatorSeekBar scale;
    @BindView(R.id.coupon_left)
    TextView couponLeft;
    @BindView(R.id.coupon_right)
    TextView couponRight;
    private List<CouponBean> couponBeanList = new ArrayList<>();
    private List<MyCounponBean.NextInterJson> nextInterList = new ArrayList<>();
    private CouponListAdapter couponListAdapter;
    private NextCouponListAdapter nextCouponListAdapter;
    private int position;

    @NonNull
    @Override
    public MyVipPresenter createPresenter() {
        return new MyVipPresenter();
    }

    @Override
    protected String setTitle() {
        return getResources().getString(R.string.string_my_vip);
    }

    @Override
    protected void showBack(TextView button_goback) {
        button_goback.setVisibility(View.VISIBLE);
        button_goback.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        button_goback.setTextColor(getResources().getColor(R.color.main_color));
        button_goback.setText(getResources().getString(R.string.back_normal) + getResources().getString(R.string.back));
        button_goback.setTypeface(iconfont);
    }

    @NonNull
    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_vip;
    }

    @Override
    protected void initEvent() {
//        scale.setUserSeekAble(false);
        seekBar.setFourRoundRect(true);
        seekBar.setBackgroundColor(getResources().getColor(R.color.color_1f1a17));
        seekBar.setProgressColor(getResources().getColor(R.color.color_C8B183));
        seekBar.setTextColor(getResources().getColor(R.color.color_C8B183));
        nextBtn.setTypeface(iconfont);
        nextBtnLevel.setTypeface(iconfont);
        myVipList.setLayoutManager(UIUtils.getRecyclerViewManager(false, this));
        levelVipList.setLayoutManager(UIUtils.getRecyclerViewManager(false, this));
        couponBeanList.clear();
        nextInterList.clear();
        couponListAdapter = new CouponListAdapter(this, iconfont);
        nextCouponListAdapter = new NextCouponListAdapter(this, iconfont);
        myVipList.setAdapter(couponListAdapter);
        //我的权益
        couponListAdapter.OnItemClick(new CouponListAdapter.OnItemClick() {
            @Override
            public void setOnItemClick(View view, int pos) {
                //调用领取权益
                position = pos;
                CouponBean couponBean = couponBeanList.get(pos);
                getPresenter().getiInterestsIdDat("" + couponBean.getInterestsId());
            }
        });
        levelVipList.setAdapter(nextCouponListAdapter);
        //下一级权益
        nextCouponListAdapter.OnItemClick(new NextCouponListAdapter.OnItemClick() {
            @Override
            public void setOnItemClick(View view, int pos) {
                //调用领取权益
                MyCounponBean.NextInterJson interJson = nextInterList.get(pos);
                getPresenter().getiInterestsIdDat(""+interJson.getNextInterestsId());
            }
        });

        getPresenter().getVip();
    }

    @Override
    public void showVip(MyCounponBean.MyCouponJson myCouponJson) {
        LoadingDialog.get().hideLoading();
        if (myCouponJson.getMember() != null) {
            MyCounponBean.MemberJson memberJson = myCouponJson.getMember();
            seekBar.setText("" + memberJson.getValue());//当前经验阈值
//            scale.setProgress(memberJson.getValue());
//            scale.setMax(memberJson.getNextThreshold());
            couponLeft.setText(""+memberJson.getThreshold());
            couponRight.setText(""+memberJson.getNextThreshold());
            float progress = (float) memberJson.getValue() / memberJson.getNextThreshold() * 100;
            if(memberJson.getValue()==0 || progress<1){
                seekBar.setProgress(6);//进度值计算
            }else{
                seekBar.setProgress(progress);//进度值计算
            }
            vipName.setText(memberJson.getMemberLevelName()+getResources().getString(R.string.vip));//当前会员等级名称
            updateTxt.setText(getResources().getString(R.string.level_one)
                    + (memberJson.getNextThreshold() - memberJson.getValue())
                    + getResources().getString(R.string.level_two)
                    + memberJson.getNextMemberLevelName()+getResources().getString(R.string.vip));
            switch (memberJson.getMemberLevelName()){//左边图标
                case "黄金":
                    couponLeft.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.img_hy_jin,0,0);
                    break;
                case "白银":
                    couponLeft.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.img_hy_yin,0,0);
                    break;
                case "青铜":
                    couponLeft.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.img_hy_tong,0,0);
                    break;
                case "钻石":
                    couponLeft.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.img_hy_zuanshi,0,0);
                    break;
            }
            switch (memberJson.getNextMemberLevelName()){//右边图标
                case "黄金":
                    couponRight.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.img_hy_jin,0,0);
                    break;
                case "白银":
                    couponRight.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.img_hy_yin,0,0);
                    break;
                case "青铜":
                    couponRight.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.img_hy_tong,0,0);
                    break;
                case "钻石":
                    couponRight.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.img_hy_zuanshi,0,0);
                    break;
            }

        }
        if (myCouponJson.getInterests().size() > 0) {//我的权益列表数据
            couponBeanList.addAll(myCouponJson.getInterests());
            couponListAdapter.setData(couponBeanList);
            couponListAdapter.notifyDataSetChanged();
        }
        if (myCouponJson.getNextInterests().size() > 0) {//下一级权益列表数据
            nextInterList.addAll(myCouponJson.getNextInterests());
            nextCouponListAdapter.setData(nextInterList);
            nextCouponListAdapter.notifyDataSetChanged();
        }
    }

    /**
     * 领取优惠券的返回
     *
     * @param baseJSON
     */
    @Override
    public void getInterJsonData(BaseJSON baseJSON) {
        ToastUtil.getToastEmail().ToastShow(this, null, 1, R.layout.toast_view, getResources().getString(R.string.coupon_get));
        for (int i = 0; i < couponBeanList.size(); i++) {
            if (i == position) {
                couponBeanList.get(i).setIsGet(1);
            }
        }
        couponListAdapter.setData(couponBeanList);
        couponListAdapter.notifyDataSetChanged();
    }
}
